<?php

/**
 * Created by PhpStorm.
 * User: jc234839
 * Date: 27/04/2014
 * Time: 2:52 PM
 */
class Product
{
    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    public function insertProduct($productName, $productDescription, $unitPrice)
    {
        //if($this->getProductID($ProductName) !== false) return "Product already exists in Database";

        $sql = "INSERT INTO Products ('ProductName', 'ProductDescription', 'UnitPrice') VALUES (?, ?, ?)";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $productName);
        $query->bindValue(2, $productDescription);
        $query->bindValue(3, $unitPrice);

        try {
            $query->execute();

            if ($query) return "Product was successfully inserted into the database!";

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return "Product was NOT inserted into the database.";
    }

    public function updateProduct($productID, $productName, $productDescription, $unitPrice, $isFeatured)
    {
        $sql = "UPDATE Products SET ProductName = ?, ProductDescription = ?, UnitPrice = ?, IsFeatured = ? WHERE ProductID = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $productName);
        $query->bindValue(2, $productDescription);
        $query->bindValue(3, $unitPrice);
        $query->bindValue(4, $isFeatured);
        $query->bindValue(5, $productID);

        try {
            $query->execute();
            if ($query) return "$productName was updated successfully!";

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return "$productName was not updated successfully!";
    }

    public function deleteProduct($productID)
    {
        $sql = "UPDATE Products SET EOLDate = ? WHERE ProductID = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, date('Y-m-d'));
        $query->bindValue(2, $productID);

        try {
            $query->execute();

            if ($query) {
                $sql = "DELETE FROM Inventory WHERE ProductID = ?";
                $query = $this->db->prepare($sql);

                $query->bindValue(1, $productID);

                try {
                    $query->execute();

                    if ($query) {
                        $sql = "DELETE FROM Images WHERE ProductID = ?";
                        $query = $this->db->prepare($sql);

                        $query->bindValue(1, $productID);

                        try {
                            $query->execute();

                            if ($query) return "Item was deleted from the database successfully!";

                        } catch (PDOException $e) {

                            echo $e->getMessage();
                        }
                    }

                } catch (PDOException $e) {
                    echo $e->getMessage();
                }
            }

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return "Item was NOT deleted from the database successfully.";
    }

    public function uploadImage($productName, $fileName)
    {
        $allowedExts = array('gif', 'jpeg', 'jpg', 'png');
        $temp = explode(".", $fileName);
        $extension = end($temp);

        $productID = $this->getProductID($productName);
        $imageName = $productID . $_FILES["file"]["name"];

        if (in_array($extension, $allowedExts)) {
            if ($_FILES["file"]["error"] > 0) {
                return "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                if (file_exists("upload/" . $imageName)) {
                    return $imageName . " already exists.";
                } else {

                    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $imageName);

                    $path = "upload/" . $imageName;

                    $sql = "INSERT INTO Images (ProductID, ImageName, Path) VALUES (?, ?, ?)";

                    $query = $this->db->prepare($sql);

                    $query->bindValue(1, $productID);
                    $query->bindValue(2, $imageName);
                    $query->bindValue(3, $path);

                    try {
                        $query->execute();

                        return "Image successfully uploaded";

                    } catch (PDOException $error) {
                        echo $error->getMessage();
                    }
                }
            }
        }
        return "Unable to upload Image";
    }

    public function updateInventory($inventoryID, $colour, $size, $unitsInStock)
    {
        $sql = "UPDATE Inventory SET Colour = ?, Size = ?, UnitsInStock = ? WHERE InventoryID = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $colour);
        $query->bindValue(2, $size);
        $query->bindValue(3, $unitsInStock);
        $query->bindValue(4, $inventoryID);

        try {
            return $query->execute();

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return false;
    }

    private function getProductID($productName)
    {
        $sql = "SELECT ProductID FROM Products WHERE ProductName = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $productName);

        try {
            $query->execute();
            $record = $query->fetch();

            return $record['ProductID'];

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        return false;
    }
} 